package Shopping;

import java.util.Scanner;

public class shoppingTist {
    public static void main(String[] args) {

        Shopping n1 = new Shopping("可乐",10,1 );
        Shopping n2 = new Shopping("果汁",2,2 );
        Shopping n3 = new Shopping("苹果",3,4 );
        Shopping n4 = new Shopping("香蕉",4,3 );

        Scanner scanner = new Scanner(System.in);
        System.out.println("\n--- 购物车系统 ---");
        System.out.println("1. 查看商品列表");
        System.out.println("2. 添加商品到购物车");
        System.out.println("3. 查看购物车");
        System.out.println("4. 从购物车中删除商品");
        System.out.println("5. 结算");
        System.out.println("6. 退出");
        System.out.print("请选择操作: ");

        Car[] cart = new Car[20]; // 创建一个大小为20的购物车数组
        int cartSize = 0; // 跟踪当前购物车中的项数

        while (true){
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            if(choice==1){
                n1.list();
                n2.list();
                n3.list();
                n4.list();
            }
            if(choice==2){
                addToCart(cart, cartSize, n1, n2, n3, n4);
                cartSize++; // 增加购物车大小
            }
            if (choice==3){
                listCart(cart, cartSize);
            }
            if (choice==4){
                deliettoCart(cart,cartSize);
            }
            if (choice==5){
                resulttoCart( cart,  cartSize);
            }
            if (choice==6){
                break;
            }
        }

    }
    public static void addToCart(Car[] cart, int cartSize, Shopping... items) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入商品名称");
        String name = scanner.next();
        System.out.println("请输入数量");
        int quantity = scanner.nextInt();

        boolean found = false;
        for (int i = 0; i < cartSize; i++) {
            if (cart[i].getName().equals(name)) {
                cart[i].setNum(quantity); // 增加现有商品的数量
                found = true;
                break;
            }
        }

        if (!found && cartSize < cart.length) {
            cart[cartSize] = new Car(name, quantity); // 添加新商品到购物车
            System.out.println("已成功添加");
        }
        else {
            System.out.println("购物车已满或商品不存在");
        }
    }

    public static void listCart(Car[] cart, int cartSize) {
        for (int i = 0; i < cartSize; i++) {
            cart[i].list();
        }
    }

    public static void deliettoCart(Car[] cart, int cartSize){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择删除的商品名称");
        String name = scanner.next();
        for (int i = 0; i < cartSize; i++) {
            if(cart[i].getName().equals(name)){
                cart[i] = cart[19];
                System.out.println("删除成功");
            }else {
                System.out.println("不存在该商品");
            }
        }
    }

    public static void resulttoCart(Car[] cart, int cartSize){
        int sum=0;
        for (int i = 0; i < cartSize; i++) {
            if(cart[i].getName().equals("可乐")){
                sum+=cart[i].getNum()*10;
            }
            if(cart[i].getName().equals("果汁")){
                sum+=cart[i].getNum()*2;
            }
            if(cart[i].getName().equals("香蕉")){
                sum+=cart[i].getNum()*4;
            }
            if(cart[i].getName().equals("苹果")){
                sum+=cart[i].getNum()*3;
            }
        }
        System.out.println("总价："+sum);
    }
}


